package com.dqy.twserver.server.handle.ehandle.mutil;

import com.dqy.twserver.server.dto.GameDto;
import com.dqy.twserver.server.handle.ehandle.GameEventHandle;
import com.dqy.twserver.server.support.Constans;
import com.dqy.twserver.server.support.MsgPush;
import com.dqy.twserver.server.support.Player;
import com.dqy.twserver.server.support.SupportMap;
import com.dqy.twserver.server.support.data.RoomInfo;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class GameEventReadiHandle implements GameEventHandle ,IRoom{
    static Logger logger = LoggerFactory.getLogger(GameEventReadiHandle.class);
    @Override
    public void handle(GameDto req, NioSocketChannel channel) throws Exception {
        logger.info("[GameEventReadiHandle] start");

        GameDto resp=new GameDto();
        String playerName = req.getPlayerName();
        String roomId = req.getRoomId(); //#RID+rid
        int rid=roomIdToRid(roomId);
        logger.info("[GameEventReadiHandle] playerName={},roomId={},rid={}",playerName,roomId,rid);

        Player player = SupportMap.playerMap.get(playerName);
        RoomInfo roomInfo = SupportMap.roomMap.get(rid);
        roomInfo.getPlayerList().stream().forEach(i->{
            if(playerName.equals(i.getPlayerName())){
                i.setPlayReadi(1);
                logger.info("[GameEventReadiHandle] 玩家{}已准备就绪",i.getPlayerName());
            }
        });
        resp.setSts(200);
        resp.setInfo(playerName);
        resp.setRoomId(roomId);
        channel.writeAndFlush(resp);

        String roomInfoListStr = getRoomInfo(roomInfo.getPlayerList());
        logger.info("[GameEventReadiHandle] roomListInfoStr = {}",roomInfoListStr);
        String msgBody="[系统通知]：" + playerName +"准备就绪";
        MsgPush.sendMsg(roomInfo.getPlayerList(),msgBody,"info", Constans.EVENT_TYPE_SEND_ALL_MSG);
        MsgPush.sendMsg(roomInfo.getPlayerList(),roomInfoListStr, "roomListStr",Constans.EVENT_TYPE_SEND_ROOM_INFO); //重新推送房间信息

        logger.info("[GameEventReadiHandle] end");
    }
}
